<template>
  <div id="seller" class="seller" ref="seller">
    <div class="seller-content" ref="sellerContent">
      <div class="overview">
        <div class="title-wrapper">
          <h1>{{seller.name}}</h1>
          <div class="wrapper">
            <div class="star-wrapper">
              <v-star :size="36" :score="seller.score"></v-star>
            </div>
            <span class="rating-count">({{seller.ratingCount}})</span>
            <span class="sell-count">月售{{seller.sellCount}}单</span>
          </div>
          <div class="collect" @click="collet">
            <i class="icon-favorite" :class="{'collected': collected}"></i>
            <span class="text">{{collectText}}</span>
          </div>
        </div>
        <div class="remark">
          <div class="mim-price">
            <p class="title">起送价</p>
            <p class="value">{{seller.minPrice}}</p>
            <span class="unit">元</span>
          </div>
          <div class="delivery-price">
            <p class="title">商家配送</p>
            <p class="value">{{seller.deliveryPrice}}</p>
            <span class="unit">元</span>
          </div>
          <div class="delivery-time">
            <p class="title">平均配送时间</p>
            <p class="value">{{seller.deliveryTime}}</p>
            <span class="unit">分钟</span>
          </div>
        </div>
      </div>
      <v-split></v-split>
      <div class="bulletin-activities">
        <h2>公告和活动</h2>
        <p class="bulletin">{{seller.bulletin}}</p>
        <ul>
          <li class="activity" v-for="(item, index) in seller.supports" :key="index">
            <v-icon :type="item.type" size="16px" :extra="4"></v-icon>
            <span class="text">{{item.description}}</span>
          </li>
        </ul>
      </div>
      <v-split></v-split>
      <div class="pics">
        <h2 class="title">商家实景</h2>
        <div class="pics-wrapper" ref="picsWrapper">
          <ul class="pic-list" ref="picList">
            <li class="pic-item" v-for="(pic, index) in seller.pics" :key="index">
              <img :src="pic" width="120px" height="90px">
            </li>
          </ul>
        </div>
      </div>
      <v-split></v-split>
      <div class="seller-info">
        <h1 class="title">商家信息</h1>
        <div v-show="seller.infos" class="info" v-for="(info, index) in seller.infos" :key="index">{{info}}</div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll';
import vSplit from '../split/split';
import vStar from '../star/star';
import vIcon from '../icon/icon';

export default {
  name: 'seller',
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      collected: false
    };
  },
  computed: {
    collectText() {
      if (this.collected) {
        return '已收藏';
      } else {
        return '收藏';
      }
    }
  },
  methods: {
    _initScroll() {
      if (!this.scroll) {
        this.scroll = new BScroll(this.$refs.seller, {
          click: true,
          probeType: 3
        });
      } else {
        this.scroll.refresh();
      }
    },
    _initPics() {
      if (this.seller.pics) {
        let picWidth = 120;
        let picMargin = 6;
        let width = (picMargin + picWidth) * this.seller.pics.length - picMargin;
        this.$refs.picList.style.width = width + 'px';
        this.$nextTick(() => {
          if (!this.picsScroll) {
            this.picsScroll = new BScroll(this.$refs.picsWrapper, {
              click: true,
              scrollY: false,
              scrollX: true,
              eventPassthrough: false,
              probeType: 3
            });
          } else {
            this.picsScroll.refresh();
          }
        });

      }
    },
    collet() {
      this.collected = !this.collected;
    }
  },
  watch: {
    seller() {
      console.log("watch seller.");
      this.$nextTick(() => {
        console.log(this.$refs.sellerContent.offsetHeight);
        this._initScroll();
        this._initPics();
      });
    }
  },
  mounted() {
    console.log("seller mounted.");
    this.$nextTick(() => {
      this._initScroll();
      this._initPics();
    });
  },
  components: {
    vSplit,
    vStar,
    vIcon
  }
}
</script>

<style lang="less" scoped>
@import '../../common/styles/mixin.less';

.seller {
  position: absolute;
  top: 174px;
  bottom: 0;
  left: 0;
  width: 100%;
  overflow: hidden;
  .seller-content {
    .overview {
      margin: 0 18px;
      padding: 18px 0;
      font-size: 0;
      .title-wrapper {
        position: relative;
        margin-bottom: 18px;
        h1 {
          font-size: 14px;
          font-weight: 700;
          color: rgb(7, 17, 27);
          line-height: 14px;
        }
        .wrapper {
          margin-top: 8px;
          .star-wrapper {
            display: inline-block;
            margin-right: 8px;
          }
          .sell-count,
          .rating-count {
            display: inline-block;
            vertical-align: top;
            font-size: 10px;
            color: rgb(77, 85, 93);
            line-height: 18px;
          }
          .rating-count {
            margin-right: 12px;
          }
        }
        .collect {
          position: absolute;
          top: 0;
          right: 0;
          text-align: center;
          .icon-favorite {
            display: block;
            font-size: 24px;
            color: #d4d6d9;
            margin-bottom: 4px;
            &.collected {
              color: rgb(240, 20, 20);
            }
          }

          .text {
            font-size: 10px;
          }
        }
      }

      .remark {
        padding-top: 18px;
        display: flex;
        border-top: 1px solid rgba(7, 17, 27, 0.1);
        .title {
          font-size: 10px;
          color: rgb(147, 153, 159);
          line-height: 10px;
          margin-bottom: 4px;
        }
        .value {
          font-size: 24px;
          font-weight: 200;
          color: rgb(7, 17, 27);
          display: inline-block;
          vertical-align: baseline;
        }
        .unit {
          font-size: 10px;
        }

        .delivery-price,
        .mim-price,
        .delivery-time {
          flex: 1;
          text-align: center;
          border-right: 1px solid rgba(7, 17, 27, 0.1);
        }

        .delivery-time {
          border-right: none;
        }
      }
    }
    .bulletin-activities {
      margin: 18px 18px 0 18px;
      h2 {
        font-size: 14px;
        font-weight: 700;
        color: rgb(7, 17, 27);
        line-height: 14px;
      }

      .bulletin {
        padding: 8px 12px 16px 12px;
        font-size: 12px;
        line-height: 24px;
        color: rgb(240, 20, 20);
        font-weight: 200;
      }

      .activity {
        padding: 16px 12px;
        border-top: 1px solid rgba(7, 17, 27, 0.1);
        .text {
          font-size: 12px;
          line-height: 16px;
          margin-left: 6px;
        }
      }
    }
    .pics {
      position: relative;
      padding: 18px;
      .title {
        font-size: 14px;
        font-weight: 700;
        color: rgb(7, 17, 27);
        line-height: 14px;
        margin-bottom: 12px;
      }
      .pics-wrapper {
        width: 100%;
        white-space: nowrap;
        overflow: hidden;
        .pic-list {
          font-size: 0;
          .pic-item {
            display: inline-block;
            margin-right: 6px;
            &.last-child {
              margin: 0;
            }
          }
        }
      }
    }
    .seller-info{
      padding: 18px 18px 0 18px;
      .title {
        font-size: 14px;
        font-weight: 700;
        color: rgb(7, 17, 27);
        line-height: 14px;
        margin-bottom: 12px;
      }
      .info{
        padding: 16px 12px;
        border-top: 1px solid rgba(7, 17, 27, 0.1);
        font-size: 12px;
        font-weight: 200;
        color: rgb(7, 17, 27);
        line-height: 16px;
      }
    }
  }
}
</style>